package com.yanqu.road.server.manger.robot;

import com.yanqu.road.entity.config.robot.RobotTempConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.estatewar.EstateFloorInfo;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.entity.player.*;
import com.yanqu.road.logic.bussiness.config.RobotConfigBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.passRoad.PassRoadModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.*;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.config.EstateWarConfigMgr;
import com.yanqu.road.server.manger.config.TitleMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/5/13 20:27
 */
public class RobotMgr extends TempMgr {
    private static Map<Integer, RobotTempConfig> robotTempConfigMap;
    private static RandomHelper random = new RandomHelper();

    public static int initPlayerGameData(long userId, int level, List<Long> errorUserIdList) {
        try {
            RobotTempConfig robotTempConfig = robotTempConfigMap.get(level);
            GamePlayer player = GamePlayerMgr.getPlayer(userId);
            //身份等级
            if (player == null) {
                getLogger().error("load player is null, userId:{}", userId);
                errorUserIdList.add(userId);
                return -1;
            }
            player.getUserInfo().setTitleId(robotTempConfig.getTitleId());
            int needPrestige = 0;
            if (TitleMgr.getTitleInfo(level - 1) != null) {
                needPrestige = TitleMgr.getTitleInfo(level - 1).getNeedPrestige().intValue();
            }
            //声望
            String[] prestigeConfig = robotTempConfig.getPrestigeParam().split(",");
            int prestige = random.next(Integer.parseInt(prestigeConfig[0]), Integer.parseInt(prestigeConfig[1]) + 1) + needPrestige;
            player.getUserInfo().setPrestige(BigInteger.valueOf(prestige));
            player.getModule(AttributeModule.class).onDataChange(ePlayerAttrType.Prestige);
            int patronsAddAbility = 0;
            if (!robotTempConfig.getPatronsAbilityParam().isEmpty()) {
                String[] patronsAbilityParam = robotTempConfig.getPatronsAbilityParam().split(",");
                patronsAddAbility = random.next(Integer.parseInt(patronsAbilityParam[0]), Integer.parseInt(patronsAbilityParam[1]) + 1);
            }
            //门客
            if (!robotTempConfig.getPatronsParam().isEmpty()) {
                robotPatronsLogic(robotTempConfig.getPatronsParam(), player, patronsAddAbility);
            }
            //珍兽
            if (!robotTempConfig.getVehicleParam().isEmpty()) {
                robotVehicleLogic(robotTempConfig.getVehicleParam(), player);
            }
            //美女
            if (!robotTempConfig.getBeautyParam().isEmpty()) {
                robotBeautyLogic(robotTempConfig.getBeautyParam(), player);
            }
            //乔迁风云
            if (!robotTempConfig.getEstateWarIdParam().isEmpty()) {
                robotEstateWarLogic(robotTempConfig.getEstateWarIdParam(), player);
            }
            //钱庄
            if (!robotTempConfig.getHouseLvParam().isEmpty()) {
                robotHouseLvLogic(robotTempConfig.getHouseLvParam(), player);
            }
            //建筑
            if (!robotTempConfig.getBuildParam().isEmpty()) {
                robotBuildingLogic(robotTempConfig.getBuildParam(), player);
            }
            //关卡
            if (!robotTempConfig.getPassParam().isEmpty()) {
                robotPassLogic(robotTempConfig.getPassParam(), player);
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            errorUserIdList.add(userId);
            return -1;
        }
    }

    private static void robotPatronsLogic(String patronsConfig, GamePlayer player, int patronsAddAbility) {
        //门客
        String[] patronsArr = patronsConfig.split(";");
        for (String patronsParams : patronsArr) {
            String[] patronsParam = patronsParams.split(",");
            int patronsId = Integer.parseInt(patronsParam[0]);
            int patronsLevel = random.next(Integer.parseInt(patronsParam[1]), Integer.parseInt(patronsParam[2]) + 1);
            player.getModule(PatronsModule.class).addPatrons(patronsId, eLogMoneyType.GM, eLogMoneyType.GmSend);
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
            userPatrons.setAddAbility(BigInteger.valueOf(patronsAddAbility));
            userPatrons.setLevel(patronsLevel);
        }
    }

    private static void robotVehicleLogic(String vehicleConfig, GamePlayer player) {
        //珍兽
        String[] vehicleArr = vehicleConfig.split(";");
        for (String vehicleParams : vehicleArr) {
            String[] vehicleParam = vehicleParams.split(",");
            int vehicleId = Integer.parseInt(vehicleParam[0]);
            int vehicleLevel = random.next(Integer.parseInt(vehicleParam[1]), Integer.parseInt(vehicleParam[2]) + 1);
            UserVehicle userVehicle = player.getModule(VehicleModule.class).addRobotVehicle(vehicleId);
            userVehicle.setLevel(vehicleLevel);
        }
    }

    private static void robotBeautyLogic(String beautyConfig, GamePlayer player) {
        //美女
        String[] beautyArr = beautyConfig.split(";");
        for (String beautyParams : beautyArr) {
            String[] beautyParam = beautyParams.split(",");
            int beautyId = Integer.parseInt(beautyParam[0]);
            int intimacy = random.next(Integer.parseInt(beautyParam[1]), Integer.parseInt(beautyParam[2]) + 1);
            int glamour = random.next(Integer.parseInt(beautyParam[3]), Integer.parseInt(beautyParam[4]) + 1);
            player.getModule(BeautyModule.class).addBeauty(beautyId, eLogMoneyType.GM, eLogMoneyType.GmSend);
            UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(beautyId);
            userBeauty.setIntimacy(intimacy);
            userBeauty.setGlamour(glamour);
        }
    }

    private static void robotEstateWarLogic(String estateWarIdParam, GamePlayer player) {
        //乔迁风云
        String[] estateWarParam = estateWarIdParam.split(",");
        List<EstateFloorInfo> estateWarPosList = new ArrayList<>();
        int beginPosParam = Integer.parseInt(estateWarParam[0]);
        int endPosParam = Integer.parseInt(estateWarParam[1]);
        Map<Integer, Map<Integer, EstateFloorInfo>> infoMap = EstateWarConfigMgr.getEstateFloorInfoMap();
        for (Map<Integer, EstateFloorInfo> estateFloorInfoMap : infoMap.values()) {
            for (EstateFloorInfo estateFloorInfo : estateFloorInfoMap.values()) {
                int estateFloorId = estateFloorInfo.getType() * 10000 + estateFloorInfo.getPos();
                int gap = Math.abs(estateFloorId - beginPosParam) + Math.abs(estateFloorId - endPosParam);
                if (gap == Math.abs(endPosParam - beginPosParam)) {
                    estateWarPosList.add(estateFloorInfo);
                }
            }
        }
        player.getModule(EstateWarModule.class).initSystem();
        randomEstateWarPos(estateWarPosList, player);
    }

    private static void randomEstateWarPos(List<EstateFloorInfo> estateWarPosList, GamePlayer player) {
        //乔迁风云随机位置
        EstateFloorInfo estateFloorInfo = estateWarPosList.get(random.next(estateWarPosList.size()));
        UserEstateWarData userEstateWarData = player.getModule(EstateWarModule.class).getUserEstateWarData();
        userEstateWarData.setPos(estateFloorInfo.getPos());
        userEstateWarData.setMaxPos(estateFloorInfo.getPos());
        userEstateWarData.setType(estateFloorInfo.getType());
        userEstateWarData.setMaxType(estateFloorInfo.getType());
    }

    private static void robotHouseLvLogic(String houseLvConfig, GamePlayer player) {
        //钱庄
        String[] houseParam = houseLvConfig.split(",");
        int houseLv = random.next(Integer.parseInt(houseParam[0]), Integer.parseInt(houseParam[1]));
        UserInfo userInfo = player.getUserInfo();
        userInfo.setHouseLv(houseLv);
        //成就、任务
        player.notifyListener(eGamePlayerEventType.HouseLevel.getValue(), userInfo.getHouseLv());
    }

    private static void robotBuildingLogic(String buildingConfig, GamePlayer player) {
        //建筑
        String[] buildArr = buildingConfig.split(";");
        for (String buildParams : buildArr) {
            String[] buildParam = buildParams.split(",");
            int buildId = Integer.parseInt(buildParam[0]);
            player.getModule(BuildingModule.class).addBuilding(buildId);
            UserBuilding userBuilding = player.getModule(BuildingModule.class).getBuildingById(buildId);
            userBuilding.setLevel(Integer.parseInt(buildParam[1]));
            int staffCount = random.next(Integer.parseInt(buildParam[2]), Integer.parseInt(buildParam[3]));
            userBuilding.setStaffCount(staffCount);
        }
    }

    private static void robotPassLogic(String passConfig, GamePlayer player) {
        //关卡
        String[] passParams = passConfig.split(",");
        int passId = random.next(Integer.parseInt(passParams[0]), Integer.parseInt(passParams[1]));
        int passProgressId = random.next(Integer.parseInt(passParams[2]), Integer.parseInt(passParams[3]));
        player.getModule(PassRoadModule.class).initPassRoadSystem();
        UserPassRoad userPassRoad = player.getModule(PassRoadModule.class).getUserPassRoad();
        userPassRoad.setPassId(passId);
        userPassRoad.setPassProgress(passProgressId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }


    @Override
    public boolean reloadConfig() throws Exception {
        robotTempConfigMap = RobotConfigBusiness.getRobotTempConfigMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

}
